package cn.edu.scnu.config;

import cn.edu.scnu.entity.MovieStats;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelReportGenerator {
    // 生成Excel报告的逻辑
    public static void generateMovieStatsReport(List<MovieStats> movieStatsList, String filePath) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Movie Stats");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] columns = {"电影ID", "当周播放量", "月播放量", "好评数", "热播指标", "总指标"};
        for (int i = 0; i < columns.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(columns[i]);
        }

        // 填充数据
        int rowNum = 1;
        for (MovieStats stats : movieStatsList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(stats.getMovieId());
            row.createCell(1).setCellValue(stats.getWeeklyViews());
            row.createCell(2).setCellValue(stats.getMonthlyViews());
            row.createCell(3).setCellValue(stats.getLikes());
            row.createCell(4).setCellValue(stats.getHotRank());
            row.createCell(5).setCellValue(stats.getTotalRank());
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        }

        workbook.close();
    }
}
